home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-04-20 | 1.5 KB | 59 lines |
- import java.awt.*;
- import java.applet.Applet;
-
- public class Metric extends Applet {
- TextField tf;
- TextField tf2;
- double convFactor;
- String toUnits;
- String fromUnits;
-
- public void init() {
- String factor = getParameter("factor");
- fromUnits = getParameter("from");
- toUnits = getParameter("to");
-
- if (fromUnits == null || toUnits == null || factor == null) {
- fromUnits = "inches";
- toUnits = "cm";
- convFactor = 2.56;
- } else {
- try {
- convFactor = new Double(factor).doubleValue();
- } catch (NumberFormatException e) {
- fromUnits = "inches";
- toUnits = "cm";
- convFactor = 2.56;
- }
- }
-
- add(new Label("Convert " + fromUnits + " to " + toUnits + ":"));
-
- tf = new TextField(10);
- add(tf);
-
- tf2 = new TextField(20);
- tf2.disable();
- tf2.setBackground(Color.gray);
- add(tf2);
- }
-
- public boolean action(Event e, Object what) {
- if (e.target == tf) {
- String s = tf.getText();
- try {
- double d = new Double(s).doubleValue();
- double result = d * convFactor;
- String toEntry = new Double(result).toString();
- tf2.setText(toEntry + " " + toUnits);
- } catch (NumberFormatException x) {
- tf2.setText("Please enter a number.");
- tf.setText("");
- }
- repaint();
- return true;
- }
- return false;
- }
- }
-